package com.tedu.myblog.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tedu.myblog.entity.Comment;
import com.tedu.myblog.queryvo.DetailedBlog;
import com.tedu.myblog.queryvo.FirstPageBlog;
import com.tedu.myblog.queryvo.RecommendBlog;
import com.tedu.myblog.service.BlogService;
import com.tedu.myblog.service.CommentService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

//首页控制器

@Controller
public class IndexController {

	@Autowired
	private BlogService blogService;

	@Autowired
	private CommentService commentService;

//    分页查询博客列表
	@GetMapping("/")
	public String index(Model model, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum,
			RedirectAttributes attributes) {
		PageHelper.startPage(pageNum, 20);
		List<FirstPageBlog> allFirstPageBlog = blogService.getAllFirstPageBlog();
		List<RecommendBlog> recommendedBlog = blogService.getRecommendedBlog();

		PageInfo<FirstPageBlog> pageInfo = new PageInfo<>(allFirstPageBlog);
		model.addAttribute("pageInfo", pageInfo);
		model.addAttribute("recommendedBlogs", recommendedBlog);

		return "index";
	}

//    搜索博客
	@PostMapping("/search")
	public String search(Model model, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum,
			@RequestParam String query) {
		PageHelper.startPage(pageNum, 1000);
		List<FirstPageBlog> searchBlog = blogService.getSearchBlog(query);
		PageInfo<FirstPageBlog> pageInfo = new PageInfo<>(searchBlog);
		model.addAttribute("pageInfo", pageInfo);
		model.addAttribute("query", query);
		return "search";
	}

//    跳转博客详情页面
	@GetMapping("/blog/{id}")
	public String blog(@PathVariable Long id, Model model) {
		DetailedBlog detailedBlog = blogService.getDetailedBlog(id);
		List<Comment> comments = commentService.listCommentByBlogId(id);
		model.addAttribute("comments", comments);
		model.addAttribute("blog", detailedBlog);
		return "blog";
	}

//    博客信息
	@GetMapping("/footer/blogmessage")
	public String blogMessage(Model model) {
		int blogTotal = blogService.getBlogTotal();
		int blogViewTotal = blogService.getBlogViewTotal();
		int blogCommentTotal = blogService.getBlogCommentTotal();
		int blogMessageTotal = blogService.getBlogMessageTotal();

		model.addAttribute("blogTotal", blogTotal);
		model.addAttribute("blogViewTotal", blogViewTotal);
		model.addAttribute("blogCommentTotal", blogCommentTotal);
		model.addAttribute("blogMessageTotal", blogMessageTotal);
		return "index :: blogMessage";
	}
}